<html>
	<head>
		<title>fly fight by qq.d.y</title>
		<script src="../util.js"></script>
		<script src="sprite.js"></script>
		<script>
			function rand(min, max)   { return Math.random() * (max - min) + min; }
			var gl, W, H, g2d;
			var key = [];
			for (var i=0; i<256; i++)   key[i] = 0;
			var stars = [];
			var sprtex;
			var player = new sprite();
			var enemy = [];
			var player_bullets = [];
			var explosions = [];
			var player_fire_delay = new timer();
			var difficulty = 100;
			function init() {
				W = document.getElementById('canvas').width;
				H = document.getElementById('canvas').height;
				gl = document.getElementById('canvas').getContext('experimental-webgl');
				if (!gl)   alert('please do not use fuck IE...');
				gl.clearColor(0, 0, 0, 1);
				
				var img = new Image();
				img.src = 'sprite.bmp';
				img.onload = function() {
					sprtex = gl.createTexture();
					gl.bindTexture(gl.TEXTURE_2D, sprtex);
					gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
					gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
					gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
					gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
					gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
					gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
					gl.bindTexture(gl.TEXTURE_2D, null);
				}
				
				g2d = new G2D(gl, W, H);
				
				// init backgroud stars
				for (var i=0; i<256; i++)   stars[i] = [rand(0,W), rand(0,H), [0,0,0]];
				// init player
				player.set_size(30,40);
				player.set_frame_rc([0,125,30,165]);
				player.set_bound_action(BA_STOP);
				player.set_bound_rect([0,0,W,H]);
				player.set_depth(0.4);
				
				//for (var i=0; i<50; i++)   add_enemy();
				
				setTimeout(loop, 30);
			}
			function add_enemy() {
				var r = parseInt(rand(0,3));
				var o = new sprite();
				o.set_size(32,32);
				o.set_bound_action(BA_HIDE);
				o.set_bound_rect([0,0,W,H]);
				o.set_depth(0.4);
				o.set_velocity(rand(0,5)-2, rand(0,5)-5.5);
				o.set_position(rand(35,W-35), H);
				o.set_aabb_shrink(1.5);
				if (r === 0) {
					o.set_frame_rc([116,1,148,32]);
				} else if (r === 1) {
					o.set_frame_rc([148,1,180,32]);
				} else if (r === 2) {
					o.set_frame_rc([180,1,212,32]);
				}
				enemy.push(o);
			}
			function add_explosion(x, y) {
				var o = new sprite();
				o.set_size(32,32);
				o.set_position(x,y);
				o.set_bound_rect([0,0,W,H]);
				o.set_animation_frame_delay(3);
				o.set_animation_type(AM_ONCE);
				var frmrc = [];
				for (var i=0; i<8; i++)   frmrc.push([i*32, 165, i*32+32, 197]);
				o.set_animation_frame_rc(frmrc);
				explosions.push(o);
			}
			function player_fire() {
				var o = new sprite();
				o.set_size(32,32);
				o.set_frame_rc([116,35,148,67]);
				var pos = player.get_position();
				o.set_position(pos[0]-2, pos[1]+20);
				o.set_velocity(0,6);
				o.set_bound_action(BA_HIDE);
				o.set_bound_rect([0,0,W,H]);
				o.set_aabb_shrink(12);
				player_bullets.push(o);	
			}
			function draw_sprite() {
				if (!sprtex)   return;
				g2d.bind_texture(sprtex);
				//g2d.image_flush(sprtex, 256, 256, [116,35,148,67], [100,100,132,132], 0.5);
				player.draw(g2d);
				for (var i in player_bullets)   player_bullets[i].draw(g2d);
				for (var i in enemy)   enemy[i].draw(g2d);
				for (var i in explosions)   explosions[i].draw(g2d);
			}
			function delete_sprite() {
				var del = [];
				for (var i in player_bullets)   if (!player_bullets[i].is_visible()) del.push(i);
				for (var i in del)   player_bullets.splice(del[i], 1);
				del = [];
				for (var i in enemy)   if (!enemy[i].is_visible())   del.push(i);
				for (var i in del)   enemy.splice(del[i], 1);
				del = [];
				for (var i in explosions)   if (!explosions[i].is_visible())   del.push(i);
				for (var i in del)   explosions.splice(del[i], 1);
			}
			function loop() {
				// handle key input
				var SPEED = 3;
				if (key['A']) {
					player.move_left(SPEED);
					player.set_size(30,44);
					player.set_frame_rc([90,35,120,80]);
				} else if (key['D']) {
					player.move_right(SPEED);
					player.set_size(30,46);
					player.set_frame_rc([90,79,120,125]);
				}
				if (key['W']) {
					player.move_up(SPEED);
					player.set_size(30,40);
					player.set_frame_rc([90,125,120,165]);
				} else if (key['S']) {
					player.move_down(SPEED);
					player.set_size(30,35);
					player.set_frame_rc([30,0,60,35]);
				}
				if (key['K'] && player_fire_delay.pass_ms() > 200) {
					player_fire();
					player_fire_delay.reset();
				}
				
				// add enemy
				if (parseInt(rand(0, difficulty)) === 0)   add_enemy();
				
				// update all sprite
				player.update();
				for (var i in player_bullets)   player_bullets[i].update();
				for (var i in enemy)   enemy[i].update();
				for (var i in explosions)   explosions[i].update();
				
				// collision detection
				for (var i in enemy) {
					var e = enemy[i];
					for (var j in player_bullets) {
						var b = player_bullets[j];
						if (e.collision(b)) {
							var pos = e.get_position();
							add_explosion(pos[0], pos[1]);
							e.set_visible(false);
							b.set_visible(false);
						}
					}
				}
			
				g2d.begin();

				// update and draw stars backgroud
				for (var i in stars) {
					var rgb = Math.random();
					stars[i][2] = [rgb, rgb, rgb];
					g2d.point(stars[i][0], stars[i][1], 0.9, stars[i][2]);
					stars[i][1] -= 2;
					stars[i][1] = stars[i][1] < 0 ? H : stars[i][1];
				}
				
				draw_sprite();
				
				g2d.end();
				
				delete_sprite();
				
				setTimeout(loop, 30);
			}
			var fire_key_down = false;
			document.onkeydown = function(e) {
				k = String.fromCharCode(e.which);
				key[k] = 1;
				if (k == 'K' && !fire_key_down) {
					fire_key_down = true;
					player_fire_delay.reset();
					// fire immediately when firekey press
					player_fire();
				}
			}
			document.onkeyup = function(e) {
				k = String.fromCharCode(e.which);
				key[k] = 0;
				var reset = false;
				if (k == 'K')   fire_key_down = false;
				if (k == 'A' || k == 'D' || k == 'W' || k == 'S')   reset = true;
				if (reset) {
					player.set_size(30, 40);
					player.set_frame_rc([0,125,30,165]);
				}
			}
		</script>
	</head>
	<body onload='init()'>
		<canvas id="canvas" style="border:none;" width='320' height='480'></canvas>
	</body>
</html>